﻿using Iworld.basis.Service;
using Iworld.Game.Model;
using Iworld.Game.Service.Backstage.Export;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Game.Service.Backstage.Import
{
    /// <summary>
    /// 获取牌轮信息
    /// </summary>
    [DataContract]
    public class GetGameChaptersImport : GetPageListCheckAdministratorImportBase
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        [DataMember]
        public DateTime? BeginTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        [DataMember]
        public DateTime? EndTime { get; set; }

        [DataMember]
        public GameChapterStatus? Status { set; get; }

        public override void CheckAllowExecuteOrNot(Main.Impl.ModelToDbContext db)
        {
            base.CheckAllowExecuteOrNot(db);
            if (!Self.Group.CanViewGameChapter)
            {
                throw new Exception("你无权查看牌轮");
            }
        }

        public PageResult<GameChapterExport> GetGameChaptersInfo(IModelToDbContextOfGame db)
        {
            int pageSize = 10;
            int startRow = pageSize * (this.PageIndex - 1);
            IQueryable<GameChapter> temp = db.GameChapters;
            if (BeginTime != null)
            {
                temp=temp.Where(x => x.CreatedTime >= (DateTime)BeginTime);
            }
            if (EndTime != null)
            {
                temp=temp.Where(x => x.CreatedTime <= (DateTime)EndTime);
            }
            if(Status!=null)
            {
                temp = temp.Where(x => x.Status == (GameChapterStatus)Status);
            }
            int totalCount = temp.Count();
            var list = temp.OrderByDescending(x => x.CreatedTime).Skip(startRow).Take(pageSize).ToList().ConvertAll(x =>
            {
                return new GameChapterExport(x,Self.Group.CanEditGameRound);
            });
            return new PageResult<GameChapterExport>(this.PageIndex, totalCount, pageSize, list);
        }
    }
}
